package fishBids.group3.behaviour.bank;

import es.upm.agents.ontology.ToNotifyWinner;
import fishBids.group3.AgBank;
import fishBids.group3.behaviour.bank.model.Account;
import fishBids.group3.common.Constants;
import fishBids.group3.utils.Utils;
import jade.content.lang.Codec;
import jade.content.onto.Ontology;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import jade.util.leap.Map;

public class BankBuyProductBehaviour extends CyclicBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4218560108006303061L;
	
	public BankBuyProductBehaviour(Codec codec, Ontology ontology, AgBank bankAg){
		this.codec = codec;
		this.ontology = ontology;
		this.bankAg = bankAg;
	}

	@Override
	public void action() {

		// Waits for sellProduct requests
		// Receive the message 
		//System.out.println("Bank>>> waiting for a buy product request"); 
		ACLMessage msg = bankAg.receive(MessageTemplate.and(
				MessageTemplate.MatchPerformative(ACLMessage.REQUEST),
				MessageTemplate.MatchProtocol(Constants.BUY_PRODUCT) )); 
		if(msg != null ){
		
				ToNotifyWinner toNotifyWinner;
				try {
					toNotifyWinner = (ToNotifyWinner) msg.getContentObject();
					AID aid = toNotifyWinner.getWinner();
					Account account = (Account) this.bankAg.getAgentsAccounts().get(aid.getName());
					if( account == null || account.getBalance() < toNotifyWinner.getProduct().getPrice() ){
//						System.out.println("Bank >>>>> the agent " + aid.getName() + " either doesn't have an account or doesn't have enough " +
//								"credit for the transaction of price " + toNotifyWinner.getProduct().getPrice() );
						Utils.sendMessage(ACLMessage.FAILURE, codec, ontology,  msg.getSender(), Constants.BUY_PRODUCT, bankAg);
						return;
					}
					account.setBalance(account.getBalance() - toNotifyWinner.getProduct().getPrice() );
					Float amount = (Float) account.getItems().get(toNotifyWinner.getProduct().getProductType().getType() );
					if(amount == null){
						amount = new Float(0);
					}
					float newAmount = amount.floatValue() + toNotifyWinner.getProduct().getAmount();
					Map itemsObtained = (Map) this.bankAg.getAgentsItemsObtained().get(aid.getName());
					Double itemsPerTypeObtained = (Double) itemsObtained.get(toNotifyWinner.getProduct().getProductType().getType());
					itemsPerTypeObtained = itemsPerTypeObtained + toNotifyWinner.getProduct().getAmount();
					itemsObtained.put(toNotifyWinner.getProduct().getProductType().getType(), itemsPerTypeObtained);
//					System.out.println("Bank >>>> new amount of product of type " + toNotifyWinner.getProduct().getProductType().getType() + ": " + 
//							newAmount);
//					System.out.println("Bank >>>> new amount accumulated of product: " 
//							+ itemsObtained.get(toNotifyWinner.getProduct().getProductType().getType()));
					account.getItems().put(toNotifyWinner.getProduct().getProductType().getType(), new Float(newAmount) );
					Utils.sendMessage(ACLMessage.INFORM, codec, ontology, msg.getSender(), Constants.BUY_PRODUCT, bankAg);
					Utils.sendGenericMessageWithObject(ACLMessage.INFORM, this.bankAg.getMarketID(), Constants.BUY_PRODUCT, this.bankAg, toNotifyWinner);
				} catch (UnreadableException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}else{
			block();
		}

	}
	
	Codec codec;
	Ontology ontology;
	AgBank bankAg;

}
